package heap

type ClassMember struct {
	// 修饰符
	accessFlags uint16
	// 名称
	name string
	// 描述符
	descriptor string
	// 泛型
	signature string
	// 注解
	annotationData []uint8 // RuntimeVisibleAnnotations_attribute
	// 所属类
	class *Klass
}

func (cm *ClassMember) Name() string {
	return cm.name
}
func (cm *ClassMember) Descriptor() string {
	return cm.descriptor
}
func (cm *ClassMember) Class() *Klass {
	return cm.class
}

func (cm *ClassMember) IsPublic() bool {
	return 0 != cm.accessFlags&ACC_PUBLIC
}
func (cm *ClassMember) IsPrivate() bool {
	return 0 != cm.accessFlags&ACC_PRIVATE
}
func (cm *ClassMember) IsProtected() bool {
	return 0 != cm.accessFlags&ACC_PROTECTED
}
func (cm *ClassMember) IsStatic() bool {
	return 0 != cm.accessFlags&ACC_STATIC
}
func (cm *ClassMember) IsFinal() bool {
	return 0 != cm.accessFlags&ACC_FINAL
}
func (cm *ClassMember) IsSynthetic() bool {
	return 0 != cm.accessFlags&ACC_SYNTHETIC
}
func (cm *ClassMember) IsAbstract() bool {
	return 0 != cm.accessFlags&ACC_ABSTRACT
}
func (cm *ClassMember) IsNative() bool {
	return 0 != cm.accessFlags&ACC_NATIVE
}

func (self *ClassMember) AccessFlags() uint16 {
	return self.accessFlags
}
func (self *ClassMember) Signature() string {
	return self.signature
}
func (self *ClassMember) AnnotationData() []byte {
	return self.annotationData
}

// 可访问
func (cm *ClassMember) isAccessibleTo(d *Klass) bool {
	if cm.IsPublic() {
		return true
	}
	c := cm.class
	if cm.IsProtected() {
		return d == c || d.IsSubClassOf(c) || c.getPackageName() == d.getPackageName()
	}
	if !cm.IsPrivate() {
		return c.getPackageName() == d.getPackageName()
	}
	return d == c
}
